package com.umeox.moto.common.utils;

import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class HeartRateControl {
 
	private static final String RATE_SWITCH_SYS = "sys/heartrate/onoff"; //1 开，0 关
	
	public static final String RATE_VALUE_SYS = "sys/heartrate/result";
	
    public static void openRate(){
        if(!isRateOpen()){
            writeSysValue(RATE_SWITCH_SYS,"1");
        }
    }

    public static void closeRate(){
        if(isRateOpen()){
            writeSysValue(RATE_SWITCH_SYS,"0");
        }
    }

    public static void closeRateForce(){
        writeSysValue(RATE_SWITCH_SYS,"0");
    }
    
    public static boolean isRateOpen(){
    	return read(RATE_SWITCH_SYS) == 1;
    }
    
    
    public static String getRateValue(){
    	return getSysValue(RATE_VALUE_SYS);
    }
    
	
	private static void writeSysValue(String sys,String value){
		Log.d("linfeifei", "begin writeSysValue strMode");
		  File file = new File(sys);
	      FileWriter mWriter;
	      try {
	            mWriter = new FileWriter(file);
	            mWriter.write(value);
	            mWriter.flush();
	            mWriter.close();
	            mWriter = null;
	            Log.d("linfeifei", "after writeSysValue strMode");
	      } catch (Exception e) {
	            e.printStackTrace();
	      }
	}
	
	
	
	private static String getSysValue(String sys){
		 File file = new File(sys);
	     FileInputStream fis = null;
	        try {
	            fis = new FileInputStream(file);
	            byte[] buf = new byte[1024];
	            int len = fis.read(buf, 0, buf.length);
	            fis.close();
	            String strMode = new String(buf, 0, 64).trim();
				Log.d("linfeifei", "getSysValue strMode-> " + strMode +", sys ->"+sys);
	           return strMode;
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return null;
	}
	
	
	public static int read(String sys_path){
		char[] buffer = new char[1024];
        int newState = 0;
        FileReader file = null;
        try {
            file = new FileReader(sys_path);
            int len = file.read(buffer, 0, 1024);
            newState = Integer.parseInt((new String(buffer, 0, len)).trim());
            Log.e("linfeifei", "read value ->"+newState +", sys_path -> "+sys_path);
            if (file != null) {
                file.close();
                file = null;
            }
        } catch (FileNotFoundException e) {
        	Log.d("linfeifei", "getSysValue FileNotFoundException-> ");
        } catch (Exception e) {
            try {
                if (file != null) {
                    file.close();
                    file = null;
                }
            } catch (IOException io) {
            	io.printStackTrace();
                Log.e("linfeifei", "", io);
            }
            Log.e("linfeifei", "", e);
        }
        return newState;
   }
}
